package com.gg.hj.service.impl;

import com.gg.common.mapper.SysUsersMapper;
import com.gg.common.pojo.SysUsers;
import com.gg.hj.search.SysRolesSearch;
import com.gg.hj.search.SysUsersSearch;
import com.gg.hj.service.SysUsersService;
import com.gg.hj.view.SysRolesView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service
public class SysUsersServiceImpl implements SysUsersService {
    @Resource
    private SysUsersMapper sysUsersMapper;

    @Override
    public SysUsers loginService(String loginId) {
        return this.sysUsersMapper.selectByLoginId(loginId);
    }

    @Override
    //TODO 用户信息查询
    public SysUsersSearch<SysUsers> queryUserAllService(SysUsersSearch<SysUsers> sysUsersSearch) throws Exception {
        PageHelper.startPage(sysUsersSearch.getPageNum(),sysUsersSearch.getPageSize());
        PageInfo<SysUsers> pageInfo = new PageInfo<SysUsers>(this.sysUsersMapper.queryUserAll());
        sysUsersSearch.setTotal(pageInfo.getTotal());
        sysUsersSearch.setRows(pageInfo.getList());
        return sysUsersSearch;
    }

    @Override
    //TODO 根据登入人主键查询具有的角
    public SysRolesSearch<SysRolesView> searchQueryByIdRoleService(SysRolesSearch<SysRolesView> sysRolesSearch) throws Exception {
        PageHelper.startPage(sysRolesSearch.getPageNum(),sysRolesSearch.getPageSize());

        //查询授权人具有的角色
        PageInfo<SysRolesView> pageInfo = new PageInfo<SysRolesView>(this.sysUsersMapper.searchQueryByIdRole(sysRolesSearch.getUserId()));
        sysRolesSearch.setTotal(pageInfo.getTotal());
        sysRolesSearch.setRows(pageInfo.getList());

        //查询授权目标具有的角色
        List<SysRolesView> sysRoles = this.sysUsersMapper.searchQueryByIdRole(sysRolesSearch.getUserTagId());
        StringBuffer getRoleId = new StringBuffer();
        for(SysRolesView r : sysRoles) {
            getRoleId.append(",").append(r.getId()).append(",");
        };
        //设置结果集属性 授权目标所有角色编号
        for(SysRolesView roles : sysRolesSearch.getRows()){
            roles.setUserTagId(sysRolesSearch.getUserTagId());
            roles.setUserTagIds(getRoleId.toString());
        }

        return sysRolesSearch;
    }


    @Override
    //TODO 授权 或 回收 角色
    public int AuthorizeOrRecycleRolesService(int id, int[] checks, int[] noChecks){
        try {
            if(noChecks!=null){//未选择的角色删除
                //删除授权目标角色
                this.sysUsersMapper.removeRoleId(id,noChecks);
            }
            if(checks!=null) {//选中的角色添加
                //查询当前授权目标拥有的角色
                List<SysRolesView> sysRolesViews = this.sysUsersMapper.searchQueryByIdRole(id);
                //把授权目标有的id存到数组里
                int[] array = new int[sysRolesViews.size()];
                for(int i=0;i<sysRolesViews.size();i++){
                    array[i] = sysRolesViews.get(i).getId();
                }
                //授权目标具有id
                Integer newNums1[] = Arrays.stream(array).boxed().toArray(Integer[]::new);
                //授权id
                Integer newNums2[] = Arrays.stream(checks).boxed().toArray(Integer[]::new);

                List<Integer> list1 = Arrays.asList(newNums1); //将newNums1数组转成list数组
                List<Integer> list2 = new ArrayList<Integer>();//用来存放2个数组中不相同的元素
                //判断授权目标是否具有授权角色编号  没有添加到 list2 集合中
                for (Integer t : newNums2) {
                    if (!list1.contains(t)) {
                        list2.add(t);
                    }
                }
                // List<Integer> list2 转 int[]
                int[] userTagId = list2.stream().mapToInt(Integer::valueOf).toArray();
                //未拥有角色编号 添加角色
                if(userTagId.length>0){
                    //添加授权目标角色
                    this.sysUsersMapper.saveRoleId(id,userTagId);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return 1;//异常返回
        }
        return 0;//正常返回
    }

    @Override
    //TODO 查询账号是否存在
    public SysUsers searchQueryUsersAccountService(String account) throws Exception {
        return this.sysUsersMapper.searchQueryUsersAccount(account);
    }

    @Override
    //TODO 添加用户账号信息
    public int insertSelectiveService(SysUsers sysUsers) {
        return this.sysUsersMapper.insertSelective(sysUsers);
    }

    @Override
    //TODO 更新个人资料
    public int updateAccountService(SysUsers sysUsers) throws Exception {
        return this.sysUsersMapper.updateByPrimaryKeySelective(sysUsers);
    }

}

